<template>
  <j-modal
    :title="title"
    :width="1300"
    :visible="visible"
    :maskClosable="false"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @close="close"
    cancelText="关闭"
  >
    <template slot="footer">
      <a-button key="back" style="padding-left: 8px; padding-right: 8px" @click="close()"> 关闭</a-button>

      <template v-if="!isCheck">
        <template v-if="isTemp || queryParam.state=='1' || queryParam.state=='3' || queryParam.state=='4'">
          <a-button type="primary" key="save" @click="saveTemp()">
            暂存
          </a-button>
          <a-button type="danger" key="tempSave" @click="saveData()">
            保存
          </a-button>
          <a-divider type="vertical" />
          <a-popconfirm v-if="queryParam.state=='1' || queryParam.state=='4'" title="确定提交吗?" @confirm="() => showConfirm()">
            <a-button type="primary">
              提交复核
            </a-button>
          </a-popconfirm>
        </template>
        <template v-else-if="queryParam.state=='2' && model.reviewer == userInfo.id">
          <a-divider type="vertical" />
          <a-button v-if="queryParam.state=='2'"
                    @click="setApproveReason('9')">
            同意
          </a-button>
          <a-button v-if="queryParam.state=='2'"
                    style="margin-top: 10px"
                    type="danger"
                    @click="setApproveReason('1')">
            拒绝
          </a-button>
        </template>
      </template>
    </template>
    <div style="margin: 0 auto" :class="{ isCheck: isCheck }">
      <a-form-model ref="form" :model="model" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
          <a-col :span="12">
            <a-form-model-item :label-col="{span:8}" :wrapper-col="{span:16}" label="问题分类" prop="quatypes">
              <a-tree-select
                show-search
                tree-node-filter-prop="title"
                placeholder="请选择问题分类"
                :replaceFields="{children:'children', title:'name', key:'id', value: 'id' }"
                v-model="model.quatypes"
                style="width: 100%"
                :tree-data="treeData"
                :disabled="disableSubmit"
                :maxTagCount="4"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :label-col="{span:8}" :wrapper-col="{span:16}" label="取证单编码">
              {{ model.number ? model.number : '系统生成' }}
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="被审计(调查)单位或个人" :label-col="{span:8}" :wrapper-col="{span:16}"
                               prop="unitName">
              <a-input v-model='model.unitName' :disabled='disableSubmit' />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-form-model-item label="审计(调查)事项" :label-col="{span:4}" :wrapper-col="{span:19}" prop="matter">
            <a-input v-model='model.matter' :disabled='disableSubmit' />
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="审计(调查)事项摘要" :label-col="{span:4}" :wrapper-col="{span:19}"
                             prop="auditAbstract">
            <a-textarea allow-clear v-model="model.auditAbstract" :disabled='disableSubmit' :rows="3" :max-length="5000"/>
            <span class="text-area">
              {{ model.auditAbstract && model.auditAbstract.length > 0 ? model.auditAbstract.length : 0 }} / {{ 5000 }}
            </span>
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="定性依据" :label-col="{span:4}" :wrapper-col="{span:19}" prop="accord">
            <a-textarea allow-clear v-model="model.accord" :disabled='disableSubmit' :rows="3" />
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="附件" :label-col="{span:4}" :wrapper-col="{span:19}">
            <RuleUpload :isShow="disableSubmit" ref='fileUpload' :fileid='model.fileId'
                        @uploadFile="uploadFile"></RuleUpload>
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="报告定性" :label-col="{span:4}" :wrapper-col="{span:19}" prop="reportQualitative">
            <a-input v-model='model.reportQualitative' :disabled='disableSubmit' />
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-form-model-item label="报告标准描述" :label-col="{span:4}" :wrapper-col="{span:19}" prop="description">
            <a-textarea allow-clear v-model="model.description" :disabled='disableSubmit' :rows="3"
                        :max-length="800" />
            <span class="text-area">
              {{ model.description && model.description.length > 0 ? model.description.length : 0 }} / {{ 800 }}
            </span>
          </a-form-model-item>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item :label-col="{span:8}" :wrapper-col="{span:16}" label="是否涉及金额">
              <a-switch checked-children="是" un-checked-children="否" v-model='model.isInvolveExt'
                        :disabled='disableSubmit' />
            </a-form-model-item>
          </a-col>
          <a-col v-if="model.isInvolveExt" :span="11">
            <a-form-model-item :label-col="{span:8}" :wrapper-col="{span:16}" label="涉及金额(万元)"
                               prop="involveMoney">
              <a-input-number :default-value="0"
                              v-model='model.involveMoney'
                              :disabled='disableSubmit'
                              :style="{width:'100%'}"
              />
              <span style="width: 50px; position: absolute; right: 8px;color: gray">万元</span>
            </a-form-model-item>

          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item :label-col="{span:8}" :wrapper-col="{span:16}" label="审计人员">
              <a-input v-model='model.auditUser' :disabled='true' />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item :label-col="{span:8}" :wrapper-col="{span:16}" label="编制日期" prop="writeDate">
              <j-date style='width:100%' v-model='model.writeDate' :disabled='disableSubmit'></j-date>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <a-divider orientation="left">
      <span style="font-size: 14px;"><a-icon type="bars" style="padding-right: 4px;" />所属疑点列表</span>
    </a-divider>
    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="false">
    </a-table>
    <template v-if="jlDataSource.length > 0">
      <a-divider orientation="left">
        <span style="font-size: 14px;"><a-icon type="bars" style="padding-right: 4px;" />复核日志列表</span>
      </a-divider>
      <a-table
        ref="table1"
        size="middle"
        bordered
        rowKey="id"
        :columns="jlColumns"
        :dataSource="jlDataSource"
        :pagination="false">
      </a-table>
    </template>
    <SelectQzApprover ref="approver" :id="queryParam.proId" @submitData="handleSubmit"></SelectQzApprover>
    <SetReason ref="setReason" @approveReason="approveData"></SetReason>
    <!-- table区域-end -->
  </j-modal>
</template>
<script>
import { postAction, getAction } from '@/api/manage'
import RuleUpload from '../../../../components/RuleUpload'
import store from '@/store/'
import columns from '@views/sj/sjxmzygl/commView/sssj/sjyd/qzIndexColumns'
import jlColumns from '@views/sj/sjxmzygl/commView/sssj/sjyd/qzjlIndexColumns'
import { currentYMD } from '@/mixins/commontime'
import { butStatus } from '@views/sj/sjxmzygl/butStatus'
import SelectQzApprover from '@views/sj/sjxmzygl/commView/sssj/sjqz/SelectQzApprover.vue'
import viewTypeId from '@views/sj/sjxmzygl/commView/public/typeId'
import SetReason from '@views/sj/sjxmzygl/commView/public/SetReason.vue'

export default {
  name: 'QzModel',
  props: ['id', 'bsjdwName', 'bsjdw', 'protype', 'type'],
  components: {
    SetReason,
    SelectQzApprover,
    RuleUpload
  },
  mixins: [butStatus],
  data() {
    return {
      columns: columns(this),
      jlColumns: jlColumns(this),
      dataSource: [],
      jlDataSource: [],
      viewTypeId: viewTypeId,
      typeId: viewTypeId['审计取证'],
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      disableSubmit: false,
      title: '',
      visible: false,
      isCheck: false,
      model: {},
      queryParam: {},
      regulation: [],
      treeData: [],
      userInfo: store.getters.userInfo,
      deptInfo: store.getters.departInfo,
      rules: {
        quatypes: [{ required: true, message: '不能为空!' }],
        matter: [{ required: true, message: '不能为空!' }],
        auditAbstract: [{ required: true, message: '不能为空!' }],
        description: [{ required: true, message: '不能为空!' }],
        reportQualitative: [{ required: true, message: '不能为空!' }],
        writeDate: [{ required: true, message: '不能为空!' }],
        // unitName: [{ required: true, message: '不能为空!' }],
        accord: [{ required: true, message: '不能为空!' }],
        involveMoney: [{ required: true, message: '不能为空!' }]
      },
      url: {
        regulation: '/sjzszhly/knowledgeRegulation/queryKnowledgeRegulation',
        save: '/sjxmzygl/sjWorkZtfaEvidence/add',
        saveTemp: '/sjxmzygl/sjWorkZtfaEvidence/temporarilyStore',
        tree: '/sys/category/problemTreeBytype',
        queryById: '/sjxmzygl/sjWorkZtfaEvidence/queryById',
        submit: '/sjxmzygl/sjWorkUsers/submitAuditEvidence',
        approve: '/sjxmzygl/sjWorkUsers/zyEvidenceCommit'
      },
      isTemp: true // 是否是暂存
    }
  },
  created() {
    this.getRegulation()
    this.loadTree()
  },
  watch: {},
  methods: {
    /** 疑点新增取证 **/
    addByYd(selectRows, record) {
      this.visible = true
      this.dataSource = []
      this.jlDataSource = []
      this.dataSource = selectRows

      this.model = {}
      this.queryParam = record
      this.model.auditUser = this.userInfo.realname
      this.model.writeDate = currentYMD()
      this.model.unitName = this.deptInfo.departName

      this.isTemp = true
    },
    add(record) {
      this.visible = true
      this.model = {}
      this.dataSource = []
      this.jlDataSource = []
      this.queryParam = record
      this.model.auditUser = this.userInfo.realname
      this.model.writeDate = currentYMD()
      this.model.unitName = this.deptInfo.departName

      this.isTemp = true
    },
    check(record) {
      this.model = {}
      this.dataSource = []
      this.jlDataSource = []
      this.queryParam = record
      this.visible = true
      this.isTemp = false
      this.getQzData()
    },
    edit(record) {
      this.model = {}
      this.dataSource = []
      this.jlDataSource = []
      this.queryParam = record
      this.visible = true
      this.isTemp = record.state == '0'
      this.getQzData()
    },
    getRegulation() {
      getAction(this.url.regulation).then((res) => {
        if (res.success && res.result.length > 0) {
          this.regulation = res.result
        } else {
          console.log(res)
        }
      })
    },

    loadTree() {
      this.treeData = []
      getAction(this.url.tree, { protype: this.protype }).then((res) => {
        if (res.success) {
          this.treeData = res.result[0].children
          this.treeData.forEach(item => item.selectable = false)
        }
      })
    },

    close() {
      this.visible = false
    },

    saveTemp() {
      this.isTemp = true
      this.$refs.fileUpload.saveFile('sjqz')
    },

    saveData() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.isTemp = false
          this.$refs.fileUpload.saveFile('sjqz')
        } else {
          return false
        }
      })
    },

    uploadFile(id) {
      this.model.fileId = id
      if (this.isTemp) {
        // this.model.accord = this.model.accord ? this.model.accord.toString() : null
        this.model.quatypes = this.model.quatypes ? this.model.quatypes.toString() : null
        this.model.ids = this.dataSource.map(item => item.id).join(',')
        this.model.involveMoney = this.model.isInvolveExt ? this.model.involveMoney : null
        this.model.isInvolveMoney = this.model.isInvolveExt ? '1' : '0'
        postAction(this.url.saveTemp, {
          ...this.model,
          proId: this.queryParam.proId
        }).then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
            this.visible = false
          } else {
            this.$message.error(res.message)
          }
        })
      } else {
        this.$refs.form.validate((valid) => {
          if (valid) {
            // this.model.accord = this.model.accord.toString()
            this.model.quatypes = this.model.quatypes.toString()
            this.model.ids = this.dataSource.map(item => item.id).join(',')
            this.model.involveMoney = this.model.isInvolveExt ? this.model.involveMoney : null
            this.model.isInvolveMoney = this.model.isInvolveExt ? '1' : '0'
            postAction(this.url.save, {
              ...this.model,
              proId: this.queryParam.proId
            }).then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.visible = false
              } else {
                this.$message.error(res.message)
              }
            })
          } else {
            return false
          }
        })
      }
    },

    getQzData() {
      getAction(this.url.queryById, { id: this.queryParam.id }).then((res) => {
        if (res.success && res.result) {
          this.model = res.result
          this.dataSource = res.result.yidianVoList
          this.jlDataSource = res.result.evidenceLogList
          this.$set(this.model, 'isInvolveExt', this.model.isInvolveMoney == '1')

          /*if (this.model.accord) {
            this.model.accord = this.model.accord.split(',')
          } else {
            this.model.accord = []
          }*/

          if (this.model.quatypes) {
            this.model.quatypes = this.model.quatypes.split(',')
          } else {
            this.model.quatypes = []
          }
        } else {
          this.$message.error('获取数据异常！')
          this.visible = false
        }
      })
    },

    // 提交审核
    handleSubmit(userId) {
      getAction(this.url.submit, {
        proId: this.queryParam.proId,
        userId: userId,
        typeId: this.typeId,
        evidenceId: this.model.id
      }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok')
          this.close()
        } else {
          this.$message.error(res.message)
        }
      })
    },

    // 同意拒绝
    approveData(reason) {
      let remark = this.approveStatus === '9' ? `同意：${reason}` : `拒绝：${reason}`
      getAction(this.url.approve, {
        proId: this.queryParam.proId,
        status: this.approveStatus,
        remark: remark,
        typeId: this.typeId,
        evidenceId: this.model.id
      }).then((res) => {
        if (res.success) {
          this.$refs.setReason.setApproveReason = false
          this.$emit('ok')
          this.close()
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
@import '../../../../css/checkForm.less';

.name-type {
  width: 110px;
  margin-left: 5px;
  border: 1px solid #e0e0e0;

  /deep/ .ant-select-selection {
    box-sizing: border-box;
    min-height: 27px !important;

    .ant-select-selection__rendered {
      line-height: 27px !important;
    }
  }
}

.text-area {
  margin-bottom: 0;
  position: absolute;
  right: 10px;
  bottom: 0px;
  z-index: 1;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.3);
}

</style>
